namespace my_list
{
	template<class T>
	struct list_node
	{
		list_node(const T& data = T())
			:_prev(nullptr)
			, _next(nullptr)
			, _val(data)
		{}

		list_node* _prev;
		list_node* _next;
		T _val;
	};

	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef list_node<T> Node;
		typedef __list_iterator<T, Ref, Ptr> Self;

		Node* _node;

		__list_iterator(Node* node)
			:_node(node)
		{}

		Self& operator++() { _node = _node->_next; return *this; }
		Self operator++(int) 
		{ 
			Self tmp(_node);
			_node = _node->_next; 
			return tmp;
		}

		Self& operator--() { _node = _node->_prev; return *this; }
		Self operator--(int)
		{
			Self tmp(_node);
			_node = _node->_prev;
			return tmp;
		}

		Ref operator*() { return _node->_val; }

		Ptr operator->() { return &(_node->_val); }

		bool operator!=(const Self& it) { return _node != it._node; }
		bool operator==(const Self& it) { return _node == it._node; }
	};

	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;

		iterator begin() { return iterator(_head->_next); }
		iterator end() { return iterator(_head); }
		const_iterator begin() const { return const_iterator(_head->_next); }
		const_iterator end() const { return const_iterator(_head); }
 
		void swap(list<T>& lt)
		{
			//Node* tmp = _head;
			//_head = lt._head;
			//lt._head = tmp;

			//size_t tmpsize = _size;
			//_size = lt._size;
			//lt._size = tmpsize;

			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}

		list() { list_init(); }

		template<class InputIterator>
		list(InputIterator first, InputIterator last)
		{
			list_init();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		list(const list<T>& lt)
		{
			list_init();
			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}

		list<T>& operator=(list<T> lt) { swap(lt); return *this; }

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		void list_init()
		{
			_head = new Node();
			_head->_prev = _head;
			_head->_next = _head;
			_size = 0;
		}

		void push_back(const T& val)
		{
			//Node* tail = _head->_prev;
			//Node* newnode = new Node(val);
			//tail->_next = newnode;
			//newnode->_prev = tail;
			//newnode->_next = _head;
			//_head->_prev = newnode;

			insert(end(), val);
		}

		void push_front(const T& val) { insert(begin(), val); }
		void pop_back() { erase(--end()); }
		void pop_front() { erase(begin()); }

		iterator insert(iterator pos, const T& val)
		{
			Node* newnode = new Node(val);
			Node* posnode = pos._node;
			Node* prev = posnode->_prev;
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = posnode;
			posnode->_prev = newnode;
			
			++_size;

			return iterator(newnode);
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());

			Node* posnode = pos._node;
			Node* prev = posnode->_prev;
			Node* next = posnode->_next;
			delete posnode;
			prev->_next = next;
			next->_prev = prev;

			--_size;
	
			return iterator(next);
		}

		void clear()
		{
			iterator head = begin();
			while (!empty())
				head = erase(head);
		}

		bool empty() { return _head->_next == _head; }
		size_t size() const { return _size; }

	private:
		Node* _head;
		size_t _size;
	};
}
